﻿using Microsoft.EntityFrameworkCore;
using MododorWebAPI.Models;

namespace MododorWebAPI.DbContextConfig
{
    public class MododorDbContext:DbContext
    {
        public MododorDbContext(DbContextOptions<MododorDbContext> options) : base(options) { }
        public DbSet<UserModel> User { get; set; }
        public DbSet<CompanyModel> Company { get; set; }
        public DbSet<ClientModel> Client { get; set; }
        public DbSet<ContractModel> Contract { get; set; }
        public DbSet<ContractTypeModel> ContractType { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            
            modelBuilder.Entity<UserModel>().HasKey(x=>x.idUser);//账户信息主键

            modelBuilder.Entity<CompanyModel>().HasKey(x=>x.id);//公司信息主键
            modelBuilder.Entity<CompanyModel>().HasIndex(x=>x.IdCompany).IsUnique();//公司信息idCompany唯一约束

            modelBuilder.Entity<ClientModel>().HasKey(x=>x.idClient);//公司员工信息主键

            modelBuilder.Entity<ContractModel>().HasKey(x=>x.Id);//合同信息主键
            modelBuilder.Entity<ContractModel>().HasIndex(x=>x.idContract).IsUnique();//合同信息唯一约束

            modelBuilder.Entity<ContractTypeModel>().HasKey(x=>x.idContractType);//合同类型信息主键

            modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly);
        }

    }
}
